# Basic .bash_alias file for command line shortcuts
# Save this as ~/.bash_aliases
# On your ~/.bashrc add `. ~/.bash_aliases`
# Reboot or relogin
# To try without reboot (on current bash session), run `touch ~/.bash_aliases`


## ---- Package Manager based... ---- ##
if [ -x "$(command -v apt)" ]; then
	# Debian based
	alias update-system='sudo apt update && sudo apt upgrade -y ; sync'
	alias install-package='sudo apt install -y '
	alias remove='sudo apt autoremove -y '
	alias search='apt search '
	alias clean-packages='sudo apt clean'
elif [ -x "$(command -v pacman)" ]; then
	# Arch linux based
	alias update-system='sudo pacman -Syu --noconfirm ; sync'
	alias update-mirrors='sudo reflector --verbose --latest 5 \
 --sort rate --save /etc/pacman.d/mirrorlist'
	alias install-package='sudo pacman --noconfirm -S '
	alias remove='sudo pacman --noconfirm -Rs '
	alias search='pacman -Ss '
	alias clean-packages='sudo pacman -Scc'
	alias find-pacs='find /etc -regextype posix-extended \
-regex ".+\.pac(new|save)" 2> /dev/null'
elif [ -x "$(command -v xbps-install)" ]; then
	# Void Linux based
	alias update-system='sudo xbps-install -Syu ; sync'
	alias install-package='sudo xbps-install -y '
	alias remove='sudo xbps-remove -Ry '
	alias search='xbps-query -Rs '
	alias clean-packages='sudo xbps-remove -O'
elif [ -x "$(command -v zypper)" ]; then
	# Suse based
	alias update-system='sudo zypper refresh && sudo zypper refresh ; sync'
	alias install-package='sudo zypper install -y '
	alias remove='sudo zypper remove -y '
	alias search='zypper search '
	alias clean-packages='sudo zypper clean metadata && sudo zypper clean packages'
elif [ -x "$(command -v rpm)" ]; then
	# RPM based
	alias update-system='sudo yum -y update ; sync'
	alias install-package='sudo yum install -y '
	alias remove='sudo yum remove -y '
	alias search='yum search '
	alias clean-packages='sudo yum clean metadata && sudo yum clean packages'
elif [ -x "$(command -v guix)" ]; then
	# Guix/GuixSD based
	alias update-system='guix pull && guix package -u ; sync'
	alias install-package='guix install '
	alias remove='guix remove '
	alias search='guix search '
elif [ -x "$(command -v pkg)" ]; then
	# FreeBSD systems
	alias update-system='sudo pkg update && sudo pkg upgrade -y ; sync'
	alias install-package='sudo pkg install -y '
	alias remove='sudo pkg remove -y '
	alias search='pkg search '
	alias clean-packages='sudo pkg clean -y'
fi


## ---- Service Related ---- ##
# usage: handleService start|stop|restart|enable|disable <service name>
handleService() {
	if [ -x "$(command -v systemctl)" ]; then # for SystemD
		if [ `systemctl list-unit-files -l | grep -q '^${2}.service'` ]; then
			sudo systemctl ${1} ${2}
		else
			echo "$2 service not found"
		fi
	# The "-f" check should be first for FreeBSD
	elif [ -f "/usr/sbin/service" ] || [[ "$(cat /proc/1/comm)" = "init" ]]; then # for SysVinit or "service" binary
		sudo service ${2} status # if it fails it will return non-zero value
		if [ "$?" = "0" ]; then
			sudo service ${2} ${1}
		else
			echo "$2 service not found"
		fi
	elif [[ "$(cat /proc/1/comm)" = "runit" ]]; then # for runit
		if [ -d "/etc/runit/runsvdir/current/${2}" ]; then
			if [ ${1} == 'enable' ]; then
				if [ -d '/etc/runit/sv/' ]; then # Artix
					sudo ln -s /etc/runit/sv/${2} /etc/runit/runsvdir/current/${2}
				else
					sudo ln -s /etc/sv/${2} /etc/runit/runsvdir/current/${2}
				fi
			elif [ ${1} == 'disable' ]; then
				sudo rm /etc/runit/runsvdir/current/${2}
			else
				if [ -d /etc/runit/runsvdir/current/${2} ]; then
					sudo sv ${1} ${2}
				fi
			fi
		else
			echo "$2 service not found"
		fi
	fi
}
alias start-service='handleService start'
alias stop-service='handleService stop'
alias restart-service='handleService restart'
alias enable-service='handleService enable'
alias disable-service='handleService disable'
handleServer() {
	handleService ${1} apache
	handleService ${1} apache2
	handleService ${1} httpd
	handleService ${1} mariadb
	handleService ${1} mysqld
}
alias start-server='handleServer start'
alias stop-server='handleServer stop'
alias restart-server='handleServer restart'

# killall alternative for systems without it
if [ ! -x "$(command -v killall)" ]; then
	killall () {
		echo `pidof $1` && sudo kill -9 `pidof $1` || echo "$1 not running"
	}
fi


## ---- Git Related ---- ##
git_root_command () {
	if [ -d ".git" ]; then
		if [ -z "$2" ]; then
			eval $1
		else
			# printf is to escape string/input
			eval $1 "$(printf "%q" "$2")"
		fi
	else
		echo "ERROR: Not a git repo root dir"
	fi
}
alias gtpull='git pull --ff-only'
alias gtpush='git push'
alias gts='git status'
alias gtd='git diff'
alias gtac='git_root_command "git add . && git commit -m "'
alias gtrevert='git_root_command "git reset && git checkout . && git clean -fdx"'
alias gtcl='git config --list'
alias gtc='git config'


## ---- Misc ---- ##
alias list='ls -hN --color=auto --group-directories-first'
alias update-grub='sudo grub-mkconfig -o /boot/grub/grub.cfg'
alias download-youtube='youtube-dl --write-auto-sub --embed-subs \
 -f "(mp4)[height<480]" '
alias list-groups='cut -d: -f1 /etc/group | sort'

if [ -x "$(command -v nvim)" ]; then
	alias v='nvim'
elif [ -x "$(command -v vim)" ]; then
	alias v='vim'
elif [ -x "$(command -v vi)" ]; then
	alias v='vi'
fi

alias show-metadata='exiftool '
alias strip-metadata='exiftool -all= -overwrite_original'

alias run-qemu="qemu-system-$(uname -m) -m 1024 -net nic -net user \
-soundhw all -machine accel=kvm -cdrom "

alias update-vpns='sudo python3 ~/.scripts/autovpngate.py'
alias ip-info='if [ -x "$(command -v curl)" ]; then curl -s https://ifconfig.co/json|json_pp; else wget -qO- https://ifconfig.co/json|json_pp; fi'

alias cpu-usage="awk '{u=\$2+\$4; t=\$2+\$4+\$5; if (NR==1){u1=u; t1=t;} \
else print (\$2+\$4-u1) * 100 / (t-t1) \"%\"; }' \
<(grep 'cpu ' /proc/stat) <(sleep 1;grep 'cpu ' /proc/stat)"

internal_battery_usage() { for bn in /sys/class/power_supply/BAT*; do [ -d "${bn}" ] && echo "Battery ${bn:27:1}: $(cat ${bn}/capacity)% ($(cat ${bn}/status))"; done }
alias battery-usage='internal_battery_usage'

# Budgie related
alias restart-budgie='nohup budgie-panel --replace&'

# GNOME related
alias launch-dark='setsid env GTK_THEME=Adwaita:dark '
alias start-gnome-wayland='QT_QPA_PLATFORM=wayland XDG_SESSION_TYPE=wayland dbus-run-session gnome-session'
alias start-gnome-xorg='GDK_BACKEND=x11 gnome-session'

